/*
 * @Description: 
 * @Author: yeeChen
 * @Date: 2022-12-13 20:15:08
 * @LastEditTime: 2022-12-13 20:54:21
 * @LastEditors: yeeChen
 */


const fs = require('fs')


const path = require('path')

const regStyle =/<style>[\s\S]*<\/style>/
const regScript =/<script>[\s\S]*<\/script>/

fs.readFile(path.join(__dirname, './index.html'), 'utf-8', function (err, dataStr) {
    if (err) {
        return console.log('读取文件失败'+err.message);
    }
    
    resolveCss(dataStr)
    resolveScript(dataStr)
    resolveHtml(dataStr)
})

// 定义resolveCss方法
function resolveCss(htmlStr) {
    const r1 = regStyle.exec(htmlStr)
    const newCss = r1[0].replace('<style>', '').replace('</style>', '')
    // 存储文件
    fs.writeFile(path.join(__dirname, './clock/index.css'), newCss, function (err) {
        if (err) {
            console.log('写入css样式失败'+err.message);
        }
        console.log('写入css样式成功');
    })
}

// 定义resolveJs方法
function resolveScript(htmlStr) {
    const r2 = regScript.exec(htmlStr)
    const newScript = r2[0].replace('<script>', '').replace('</script>', '')
    // 存储文件 
    fs.writeFile(path.join(__dirname, './clock/index.js'), newScript,function (err) {
        if (err) {
            return console.log('写入js文件失败'+err.message);
        }
        console.log('写入js文件成功');
    })
}

// 定义resolveHtml方法
function resolveHtml(htmlStr) {
    const newHtml = htmlStr.replace(regStyle,'<link rel="stylesheet" href="./index.css"/>').replace(regScript,'<script src="./index.js"></script>')
    // 写入文件
    fs.writeFile(path.join(__dirname, './clock/index.html'), newHtml, function (err) {
        if (err) {
            return console.log('写入文件失败'+err.message);
        }
        console.log('写入html文件成功');
    })

}